package Server.controller;

import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.ShopOrderDao;
import Server.entity.ShopMoneyList;
import Server.entity.ShopOrder;
import Server.tool.BeanToJson;
import Server.tool.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("TJController")
public class TJController {
    @Autowired
    private ShopOrderDao shopOrderDao;
    @RequestMapping("Order")
    public RetResult Order(@RequestBody JSONObject jsonObject){
        String createTime =  jsonObject.getString("createTime");
        String endTime =  jsonObject.getString("endTime");
        String userId =  jsonObject.getString("userId");
        Map<String,Object> mp  = new HashMap<>();
        if(StringUtils.isEmpty(userId)){
            List<Map<String, String>> list = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime));
            List<Map<String, String>> list1 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.NO.name()));
            List<Map<String, String>> list2 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.YES.name()));
            List<Map<String, String>> list3 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.MAIL.name()));
            List<Map<String, String>> list4 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.END.name()));
            List<Map<String, String>> list5 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.QUIT.name()));
            List<Map<String, String>> list6 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.CALL.name()));
            List<String> x=new LinkedList<>();
            List<Integer> y1=new LinkedList<>();
            List<Integer> y2=new LinkedList<>();
            List<Integer> y3=new LinkedList<>();
            List<Integer> y4=new LinkedList<>();
            List<Integer> y5=new LinkedList<>();
            List<Integer> y6=new LinkedList<>();
            int i=0;
            for(Map<String,String> lk:list){
                x.add(lk.get("0"));
                y1.add(Integer.parseInt(list1.size()-1-i>=0?list1.get(i).get("1"):"0"));
                y2.add(Integer.parseInt(list2.size()-1-i>=0?list2.get(i).get("1"):"0"));
                y3.add(Integer.parseInt(list3.size()-1-i>=0?list3.get(i).get("1"):"0"));
                y4.add(Integer.parseInt(list4.size()-1-i>=0?list4.get(i).get("1"):"0"));
                y5.add(Integer.parseInt(list5.size()-1-i>=0?list5.get(i).get("1"):"0"));
                y6.add(Integer.parseInt(list6.size()-1-i>=0?list6.get(i).get("1"):"0"));
                i++;
            }
            mp.put("x",x);
            mp.put("y1",y1);
            mp.put("y2",y2);
            mp.put("y3",y3);
            mp.put("y4",y4);
            mp.put("y5",y5);
            mp.put("y6",y6);
        }else{
            List<Map<String, String>> list = EntityUtils.listConvert(shopOrderDao.findGoupBy(createTime,endTime,userId));

            List<Map<String, String>> list1 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.NO.name(),userId));
            List<Map<String, String>> list2 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.YES.name(),userId));
            List<Map<String, String>> list3 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.MAIL.name(),userId));
            List<Map<String, String>> list4 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.END.name(),userId));
            List<Map<String, String>> list5 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.QUIT.name(),userId));
            List<Map<String, String>> list6 = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime,ShopOrder.Status.CALL.name(),userId));
            List<String> x=new LinkedList<>();
            List<Integer> y1=new LinkedList<>();
            List<Integer> y2=new LinkedList<>();
            List<Integer> y3=new LinkedList<>();
            List<Integer> y4=new LinkedList<>();
            List<Integer> y5=new LinkedList<>();
            List<Integer> y6=new LinkedList<>();
            int i=0;
            for(Map<String,String> lk:list){
                x.add(lk.get("0"));
                y1.add(Integer.parseInt(list1.size()-1-i>=0?list1.get(i).get("1"):"0"));
                y2.add(Integer.parseInt(list2.size()-1-i>=0?list2.get(i).get("1"):"0"));
                y3.add(Integer.parseInt(list3.size()-1-i>=0?list3.get(i).get("1"):"0"));
                y4.add(Integer.parseInt(list4.size()-1-i>=0?list4.get(i).get("1"):"0"));
                y5.add(Integer.parseInt(list5.size()-1-i>=0?list5.get(i).get("1"):"0"));
                y6.add(Integer.parseInt(list6.size()-1-i>=0?list6.get(i).get("1"):"0"));
                i++;
            }
            mp.put("x",x);
            mp.put("y1",y1);
            mp.put("y2",y2);
            mp.put("y3",y3);
            mp.put("y4",y4);
            mp.put("y5",y5);
            mp.put("y6",y6);
        }
        return RetResponse.makeOKRsp(mp);
    }
    @RequestMapping("money")
    public RetResult money(@RequestBody JSONObject jsonObject){
        String createTime =  jsonObject.getString("createTime");
        String endTime =  jsonObject.getString("endTime");
        String userId =  jsonObject.getString("userId");
        Map<String,Object> mp  = new HashMap<>();
        if(StringUtils.isEmpty(userId)){
            List<Map<String, String>> list = EntityUtils.listConvert(shopOrderDao.findGoup(createTime,endTime));
            List<Map<String, String>> list1 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.NO.name()));
            List<Map<String, String>> list2 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.YES.name()));
            List<Map<String, String>> list3 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.MAIL.name()));
            List<Map<String, String>> list4 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.END.name()));
            List<Map<String, String>> list5 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.QUIT.name()));
            List<Map<String, String>> list6 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.CALL.name()));
            List<String> x=new LinkedList<>();
            List<Double> y1=new LinkedList<>();
            List<Double> y2=new LinkedList<>();
            List<Double> y3=new LinkedList<>();
            List<Double> y4=new LinkedList<>();
            List<Double> y5=new LinkedList<>();
            List<Double> y6=new LinkedList<>();
            int i=0;
            for(Map<String,String> lk:list){
                x.add(lk.get("0"));
                y1.add(Double.parseDouble(list1.size()-1-i>=0?list1.get(i).get("1"):"0"));
                y2.add(Double.parseDouble(list2.size()-1-i>=0?list2.get(i).get("1"):"0"));
                y3.add(Double.parseDouble(list3.size()-1-i>=0?list3.get(i).get("1"):"0"));
                y4.add(Double.parseDouble(list4.size()-1-i>=0?list4.get(i).get("1"):"0"));
                y5.add(Double.parseDouble(list5.size()-1-i>=0?list5.get(i).get("1"):"0"));
                y6.add(Double.parseDouble(list6.size()-1-i>=0?list6.get(i).get("1"):"0"));
                i++;
            }
            mp.put("x",x);
            mp.put("y1",y1);
            mp.put("y2",y2);
            mp.put("y3",y3);
            mp.put("y4",y4);
            mp.put("y5",y5);
            mp.put("y6",y6);
        }else{
            List<Map<String, String>> list = EntityUtils.listConvert(shopOrderDao.findGoupBy(createTime,endTime,userId));

            List<Map<String, String>> list1 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.NO.name(),userId));
            List<Map<String, String>> list2 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.YES.name(),userId));
            List<Map<String, String>> list3 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.MAIL.name(),userId));
            List<Map<String, String>> list4 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.END.name(),userId));
            List<Map<String, String>> list5 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.QUIT.name(),userId));
            List<Map<String, String>> list6 = EntityUtils.listConvert(shopOrderDao.findGoupNumber(createTime,endTime,ShopOrder.Status.CALL.name(),userId));
            List<String> x=new LinkedList<>();
            List<Double> y1=new LinkedList<>();
            List<Double> y2=new LinkedList<>();
            List<Double> y3=new LinkedList<>();
            List<Double> y4=new LinkedList<>();
            List<Double> y5=new LinkedList<>();
            List<Double> y6=new LinkedList<>();
            int i=0;
            for(Map<String,String> lk:list){
                x.add(lk.get("0"));
                y1.add(Double.parseDouble(list1.size()-1-i>=0?list1.get(i).get("1"):"0"));
                y2.add(Double.parseDouble(list2.size()-1-i>=0?list2.get(i).get("1"):"0"));
                y3.add(Double.parseDouble(list3.size()-1-i>=0?list3.get(i).get("1"):"0"));
                y4.add(Double.parseDouble(list4.size()-1-i>=0?list4.get(i).get("1"):"0"));
                y5.add(Double.parseDouble(list5.size()-1-i>=0?list5.get(i).get("1"):"0"));
                y6.add(Double.parseDouble(list6.size()-1-i>=0?list6.get(i).get("1"):"0"));
                i++;
            }
            mp.put("x",x);
            mp.put("y1",y1);
            mp.put("y2",y2);
            mp.put("y3",y3);
            mp.put("y4",y4);
            mp.put("y5",y5);
            mp.put("y6",y6);
        }
        return RetResponse.makeOKRsp(mp);
    }
    @Autowired
    private EntityManager entityManager;
    @RequestMapping("Shop")
    public RetResult Shop(@RequestBody JSONObject jsonObject){
        JSONArray whereMAP =  jsonObject.getJSONArray("common");
        for (int i=0;i<whereMAP.size();i++) {
            if(whereMAP.getJSONObject(i).getString("ZDNAME").equals("user_id")){
                whereMAP.getJSONObject(i).put("ZDVAL","(select invite_id from invite_users where user_id = '"+whereMAP.getJSONObject(i).getString("ZDVAL")+"' )");
                whereMAP.getJSONObject(i).put("LIKE","9");
                break;
            }
        }

        int page = Integer.parseInt(jsonObject.getString("page"));
        int size = Integer.parseInt(jsonObject.getString("limit"));
        page = (page - 1) * size;
        String sql = "SELECT sum(count) as count, sum(money) as money, shopname, specname, price, cost, sum(money)-(cost*sum(count)) as income FROM ( SELECT c.count, c.money, d. NAME AS shopname, b. NAME AS specname, b.price, b.cost FROM ( SELECT sum(count) AS count, sum(money) money, spec_id FROM shop_order_item WHERE order_id IN ( SELECT uuid FROM shop_order WHERE ( STATUS = 'YES' OR STATUS = 'MAIL' OR STATUS = 'END' ) "+BeanToJson.bspcomm(jsonObject)+" ) GROUP BY spec_id ) AS c LEFT JOIN shop_spec AS b ON c.spec_id = b.uuid LEFT JOIN shop AS d ON b.shop_id = d.uuid ORDER BY d. NAME ) AS x GROUP BY shopname, specname, price, cost";
        List lx = entityManager.createNativeQuery(sql).getResultList();
        sql+=" limit " + page + "," + size;
        List lk = entityManager.createNativeQuery(sql).getResultList();
        return RetResponse.makeRsp(0,"成功",lk,(int)lx.size());
    }
}